<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="iccid">
              <el-input v-model="filters.iccid" clearable placeholder="iccid"></el-input>
            </el-form-item>
            <el-form-item prop="card">
              <el-input v-model="filters.card" clearable placeholder="卡号"></el-input>
            </el-form-item>
            <!--<el-form-item prop="company">
              <el-select v-model="filters.company" :clearable="true" placeholder="卡号厂商">
                <el-option
                  v-for="item in pickCardList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>-->
            <el-form-item prop="state">
              <el-select v-model="filters.state" :clearable="true" placeholder="状态">
                <el-option v-for="item in pickStates" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="iccid" label="iccid" width="200"></el-table-column>
        <el-table-column prop="card" label="卡号" width="200"></el-table-column>
        <el-table-column prop="company" label="网卡厂商" width="150" :formatter="formatCompany"></el-table-column>
        <!-- <el-table-column prop="remark" label="备注" width="150"></el-table-column> -->
        <el-table-column width="300" align="center" prop="isUsed" label="卡是否已使用">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="plain" :type="formatIsUseds(scope.row.isUsed)">
                {{ formatIsUsed(scope.row.isUsed) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createUserName" label="创建人" width="180"></el-table-column>
        <el-table-column label="状态" lign="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import EquipCardApi from '@/api/equip/equipCard';

export default {
  name: 'BoxCardManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      filters: {
        card: null,
        company: null,
        iccid: null,
        state: null,
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      pickStates: [
        { value: -1, label: '禁用' },
        { value: 0, label: '删除' },
        { value: 1, label: '启用' },
      ],
      pickCardList: [
        { value: 1, label: '中国移动' },
        { value: 2, label: '中国联通' },
        { value: 3, label: '中国电信' },
      ],
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_BOX_CARD_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_BOX_CARD_LIST_SEARCH',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_BOX_CARD_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_BOX_CARD_LIST_DELETE',
    };
  },
  components: {},
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        // 浏览器尺寸变化响应事件
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    formatState(cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue == -1) {
        return 'danger';
      } else if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    formatIsUsed(cellValue) {
      if (cellValue == 0) {
        return '否';
      } else if (cellValue == 1) {
        return '是';
      }
    },
    formatIsUseds(cellValue) {
      if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    formatCompany(row, column, cellValue) {
      if (cellValue == 1) {
        return '中国移动';
      } else if (cellValue == 2) {
        return '中国联通';
      } else if (cellValue == 3) {
        return '中国电信';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        iccid: _this.filters.iccid,
        card: _this.filters.card,
        company: _this.filters.company,
        state: _this.filters.state,
      };
      // 发送请求
      EquipCardApi.pageEquipCard(cond).then((data) => {
        this.dataList = data.data.list;
        this.page.pageTotal = data.data.total;
      });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该网卡', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.cardId,
          state: -1,
        };
        EquipCardApi.deleteEquipCard(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(val) {
      var cond = {
        id: val.cardId,
      };
      EquipCardApi.getEquipCardInfo(cond).then((data) => {
        var state = null;
        if (data.data.state == -1) {
          state = '删除';
        } else if (data.data.state === 0) {
          state = '禁用';
        } else if (data.data.state === 1) {
          state = '启用';
        }
        var company = null;
        if (data.data.company == 1) {
          company = '中国移动';
        } else if (data.data.state === 2) {
          company = '中国联通';
        } else if (data.data.state === 3) {
          company = '中国电信';
        }
        var isUsed = null;
        if (data.data.isUsed == 1) {
          isUsed = '是';
        } else {
          isUsed = '否';
        }
        var msg =
          'iccid：' +
          data.data.iccid +
          '<br/>' +
          '卡 号：' +
          data.data.card +
          '<br/>' +
          '厂 商：' +
          company +
          '<br/>' +
          '备 注：' +
          data.data.remark +
          '<br/>' +
          '卡是否已使用：' +
          isUsed +
          '<br/>' +
          '状 态：' +
          state +
          '<br/>' +
          '备 注：' +
          data.data.remark +
          '<br/>' +
          '创建时间：' +
          data.data.createDate;
        this.$alert(msg, '物 联 网 卡 详 情', {
          confirmButtonText: '确定',
          dangerouslyUseHTMLString: true,
        });
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/equip/boxCardAdd',
        query: { id: val.cardId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/equip/boxCardAdd');
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
